package com.cskaoyan.service;

import com.cskaoyan.bean.BO.BasePageInfoBo;
import com.cskaoyan.bean.PO.*;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    MarketAddressMapper marketAddressMapper;
    @Autowired
    MarketCollectMapper marketCollectMapper;
    @Autowired
    MarketFootprintMapper marketFootprintMapper;
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;
    @Autowired
    MarketFeedbackMapper marketFeedbackMapper;

    @Override
    public CommonData<MarketUser> getUserList(BasePageInfoBo basePageInfoBo, String username, String mobile) {
        PageHelper.startPage(basePageInfoBo.getPage(), basePageInfoBo.getLimit());

        MarketUserExample marketUserExample = new MarketUserExample();
        // 这里使用了${}的注入
        String OrderByClause = basePageInfoBo.getSort() + " " + basePageInfoBo.getOrder();
        marketUserExample.setOrderByClause(OrderByClause);

        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        if (username != null) {
            username = "%" + username + "%";
            criteria.andUsernameLike(username);
        }
        if (mobile != null) {
            mobile = "%" + mobile + "%";
            criteria.andMobileLike(mobile);
        }

        List<MarketUser> marketUsers = marketUserMapper.selectByExample(marketUserExample);

        PageInfo<MarketUser> pageInfo = new PageInfo<>(marketUsers);
        return CommonData.data(pageInfo);
    }

    @Override
    public MarketUser getUserDetail(Integer id) {
        return marketUserMapper.selectByPrimaryKey(id);
    }

    @Override
    public CommonData<MarketAddress> getAddressList(BasePageInfoBo basePageInfoBo, String name, Integer userId) {
        PageHelper.startPage(basePageInfoBo.getPage(), basePageInfoBo.getLimit());

        MarketAddressExample marketAddressExample = new MarketAddressExample();
        // 这里使用了${}的注入
        String OrderByClause = basePageInfoBo.getSort() + " " + basePageInfoBo.getOrder();
        marketAddressExample.setOrderByClause(OrderByClause);

        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        if (name != null) {
            name = "%" + name + "%";
            criteria.andNameLike(name);
        }
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }

        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(marketAddressExample);

        PageInfo<MarketAddress> pageInfo = new PageInfo<>(marketAddresses);
        return CommonData.data(pageInfo);
    }

    @Override
    public CommonData<MarketCollect> getCollectList(BasePageInfoBo basePageInfoBo, Integer userId, Integer valueId) {
        PageHelper.startPage(basePageInfoBo.getPage(), basePageInfoBo.getLimit());

        MarketCollectExample marketCollectExample = new MarketCollectExample();
        // 这里使用了${}的注入
        String OrderByClause = basePageInfoBo.getSort() + " " + basePageInfoBo.getOrder();
        marketCollectExample.setOrderByClause(OrderByClause);

        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();

        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null) {
            criteria.andValueIdEqualTo(valueId);
        }

        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(marketCollectExample);

        PageInfo<MarketCollect> pageInfo = new PageInfo<>(marketCollects);
        return CommonData.data(pageInfo);
    }

    @Override
    public CommonData<MarketFootprint> getFootprintList(BasePageInfoBo basePageInfoBo, Integer userId, Integer goodsId) {
        PageHelper.startPage(basePageInfoBo.getPage(), basePageInfoBo.getLimit());

        MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
        // 这里使用了${}的注入
        String OrderByClause = basePageInfoBo.getSort() + " " + basePageInfoBo.getOrder();
        marketFootprintExample.setOrderByClause(OrderByClause);

        MarketFootprintExample.Criteria criteria = marketFootprintExample.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (goodsId != null) {
            criteria.andGoodsIdEqualTo(goodsId);
        }

        List<MarketFootprint> marketFootprints = marketFootprintMapper.selectByExample(marketFootprintExample);

        PageInfo<MarketFootprint> pageInfo = new PageInfo<>(marketFootprints);
        return CommonData.data(pageInfo);
    }

    @Override
    public CommonData<MarketSearchHistory> getSearchHistoryList(BasePageInfoBo basePageInfoBo, Integer userId, String keyword) {
        PageHelper.startPage(basePageInfoBo.getPage(), basePageInfoBo.getLimit());

        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        // 这里使用了${}的注入
        String OrderByClause = basePageInfoBo.getSort() + " " + basePageInfoBo.getOrder();
        marketSearchHistoryExample.setOrderByClause(OrderByClause);

        MarketSearchHistoryExample.Criteria criteria = marketSearchHistoryExample.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (keyword != null) {
            keyword = "%" + keyword + "%";
            criteria.andKeywordLike(keyword);
        }

        List<MarketSearchHistory> marketSearchHistories = marketSearchHistoryMapper.selectByExample(marketSearchHistoryExample);

        PageInfo<MarketSearchHistory> pageInfo = new PageInfo<>(marketSearchHistories);
        return CommonData.data(pageInfo);
    }

    @Override
    public CommonData<MarketFeedback> getFeedbackList(BasePageInfoBo basePageInfoBo, Integer id, String username) {
        PageHelper.startPage(basePageInfoBo.getPage(), basePageInfoBo.getLimit());

        MarketFeedbackExample marketFeedbackExample = new MarketFeedbackExample();
        // 这里使用了${}的注入
        String OrderByClause = basePageInfoBo.getSort() + " " + basePageInfoBo.getOrder();
        marketFeedbackExample.setOrderByClause(OrderByClause);

        MarketFeedbackExample.Criteria criteria = marketFeedbackExample.createCriteria();
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (username != null) {
            username = "%" + username + "%";
            criteria.andUsernameLike(username);
        }

        List<MarketFeedback> marketFeedbacks = marketFeedbackMapper.selectByExample(marketFeedbackExample);

        PageInfo<MarketFeedback> pageInfo = new PageInfo<>(marketFeedbacks);
        return CommonData.data(pageInfo);
    }

    @Override
    public int updateUser(MarketUser marketUser) {
        int affectedRows = marketUserMapper.updateByPrimaryKey(marketUser);
        return affectedRows;
    }
}
